<?php

class commentModel extends Model {
	const DYNAMIC = 1;
	const GOODS = 2;

	public function getStatusList() {
		return [
			'待审核', '已审核', '被屏蔽',
		];
	}

	public function getTypeList() {
		return [
			1 => '动态',
			2 => '商品',
		];
	}

	public function validata($data) {
		$result = Validation::rule(
			array(Validation::isMust($data['content']), '评论内容不能为空'),
			array(Validation::isLength($data['content'], 1, 1000), '评论内容在一千个字符以内')
		);
		return $result;
	}

	public function getListWithUser($where, $value = [], $order = 'c.comment_id desc', $page = 1, $pageSize = 15) {
		$where = implode(' and ', $where);
		$limit = ($page - 1) * $pageSize;
		$offset = $pageSize;
		$total = $this->getCountWithUser($where, $value);
		$resultData = [
			'page' => intval($page),
			'pageSize' => intval($pageSize),
			'total' => $total,
			'list' => [],
		];
		if ($total <= $limit) {
			return $resultData;
		}
		$sql = "select u.nickname,u.avatar,"
			. "c.* from "
			. "wf_user u,wf_comment c where u.uid=c.uid and {$where} "
			. "order by {$order} limit {$limit}, {$offset}";
//                echo $sql;exit;
		$list = $this->query($sql, $value)->fetchAll();
		if ($list) {
			$parentCommentList = [];
			foreach ($list as $row) {
				$replyTotal = $this->getCountWithUser1($row['comment_id']);
				$originComment = [];
				$temp = $this->findByIds($row['comment_id']);
				if ($temp) {
					$originComment[] = [
						'uid' => strval($temp[0]['uid']),
						'nickname' => strval($temp[0]['nickname']),
						'avatar' => strval($temp[0]['avatar']),
						'content' => strval($temp[0]['content']),
						'imageList' => $temp[0]['image_list'] ? explode(';', $temp[0]['image_list']) : [],
						'addTime' => date('Y-m-d H:i', $temp[0]['add_time']),
					];
				}
				$resultData['list'][] = [
					'uid' => strval($row['uid']),
					'nickname' => strval($row['nickname']),
					'avatar' => strval($row['avatar']),
					'commentId' => intval($row['comment_id']),
					'content' => strval($row['content']),
					'imageList' => $row['image_list'] ? explode(';', $row['image_list']) : [],
					'upTotal' => intval($row['up_total']),
					'score' => intval($row['score']),
					'addTime' => date('Y-m-d H:i', $row['add_time']),
					'replyTotal' => $replyTotal,
					'originComment' => $originComment,
				];
			}
		}
		return $resultData;
	}

	public function findById($commentId) {
		$sql = "select u.nickname,u.avatar,c.* from wf_user u,wf_comment c where "
			. "u.uid=c.uid and c.comment_id={$commentId}";
		return $this->query($sql)->fetchRow();
	}

	public function findByIds($commentId) {
		$sql = "select u.nickname,u.avatar,c.* from wf_user u,wf_comment c where "
			. "u.uid=c.uid and c.parent_id={$commentId}";
		return $this->query($sql)->fetchAll();
	}

	public function getCountWithUser1($commentId) {
		$sql = "select count(1) as total from "
			. "wf_user u,wf_comment c where u.uid=c.uid  and c.parent_id={$commentId}";
		$result = $this->query($sql, $value)->fetchRow();
		if ($result && isset($result['total'])) {
			return intval($result['total']);
		}
		return 0;
	}

	public function getCountWithUser($where, $value = []) {
		$sql = "select count(1) as total from "
			. "wf_user u,wf_comment c where u.uid=c.uid and {$where}";
		$result = $this->query($sql, $value)->fetchRow();
		if ($result && isset($result['total'])) {
			return intval($result['total']);
		}
		return 0;
	}

	public function getListWithUser1($id) {
		$sql = "select u.nickname,u.avatar,"
			. "c.* from "
			. "wf_user u,wf_comment c where u.uid=c.uid and c.comment_id=" . $id;
//                echo $sql;exit;
		$list = $this->query($sql)->fetchAll();

		if ($list) {
			$parentCommentList = [];
			foreach ($list as $row) {
				$originComment = [];
				$temp = $this->findByIds($row['comment_id']);
				if ($temp) {
					foreach ($temp as $key => $value) {
						$originComment[] = [
							'nickname' => strval($value['nickname']),
							'avatar' => strval($value['avatar']),
							'content' => strval($value['content']),
							'imageList' => $value['image_list'] ? explode(';', $value['image_list']) : [],
							'addTime' => date('Y-m-d H:i', $value['add_time']),
						];
					}
				}
				$resultData[] = [
					'nickname' => strval($row['nickname']),
					'avatar' => strval($row['avatar']),
					'commentId' => intval($row['comment_id']),
					'content' => strval($row['content']),
					'imageList' => $row['image_list'] ? explode(';', $row['image_list']) : [],
					'upTotal' => intval($row['up_total']),
					'score' => intval($row['score']),
					'addTime' => date('Y-m-d H:i', $row['add_time']),
					'replyTotal' => count($originComment),
					'originComment' => $originComment,
				];
			}
		}
		return $resultData;
	}
}